package com.luotao.test2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient2 extends Frame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * @param args
     */
    TextField tfTxT = new TextField();
    TextArea taContent = new TextArea();
    Socket s = null;
    OutputStream dos = null;
    BufferedReader dis = null;
    private boolean bConnected = false;

    public static void main(String[] args) {
        new ChatClient2().lunachFrame();
    }

    private class RecvThread implements Runnable {

        public void run() {
            try {
                while (bConnected) {
                    String str = dis.readLine();
                    taContent.setText(taContent.getText() + str + '\n');
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void lunachFrame() {
        this.setLocation(400, 300);
        this.setSize(300, 300);
        // this.setLayout(new FlowLayout());
        this.add(tfTxT, "South");
        this.add(taContent, "North");
        pack();
        tfTxT.addActionListener(new TFListener());
        this.addWindowListener(new WindowClose());
        this.setVisible(true);
        connect();
        new Thread(new RecvThread()).start();
    }

    public void connect() {
        try {
            s = new Socket("127.0.0.1", NIOServer.port);
            dos = (s.getOutputStream());
            dis = new BufferedReader(
                    new InputStreamReader((s.getInputStream())));
            System.out.println("connected!");
            bConnected = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        try {
            dos.close();
            s.close();
        } catch (Exception e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
    }

    class WindowClose extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {
            // TODO 自动生成方法存根
            System.exit(0);
            disconnect();
        }

    }

    private class TFListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String str = tfTxT.getText().trim();// trim去掉两边空格
            // taContent.setText(str);
            tfTxT.setText("");
            try {
                byte b[] = (str + "\n").getBytes();
                System.out.println(new String(b));

                dos.write(b);
                dos.flush();
                // dos.close();

            } catch (IOException e1) {
                e1.printStackTrace();

            }
        }
    }
}

